package sketch.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import sketch.doc.SketchContainer;
import sketch.ink.SampledPoint;
import sketch.ink.StrokeContainer;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JToolBar jToolBar = null;

	private JPanel jPanel = null;

	private JLabel jLabel = null;

	private JToggleButton jToggleButtonDrawable = null;

	private JCheckBox jCheckBoxInk = null;

	private JCheckBox jCheckBoxShape = null;

	private JButton jButtonCD = null;

	private JButton jButtonSegment = null;

	private JButton jButtonParse = null;

	private JButton jButtonNew = null;

	private JButton jButtonPA = null;

	/**
	 * This method initializes jToolBar
	 *
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getJToolBar() {
		if (jToolBar == null) {
			jToolBar = new JToolBar();
			jToolBar.add(getJToggleButtonDrawable());
			jToolBar.add(getJCheckBoxInk());
			jToolBar.add(getJCheckBoxShape());

			jToolBar.setFloatable(false);
			jToolBar.add(getJButtonCD());
			jToolBar.add(getJButtonSegment());
			jToolBar.add(getJButtonPA());
			jToolBar.add(getJButtonParse());
			jToolBar.add(getJButtonNew());
		}
		return jToolBar;
	}

	/**
	 * This method initializes jPanel
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel() {

				public void paint(Graphics arg0) {
					super.paint(arg0);

					if (getJCheckBoxInk().isSelected())
						SketchContainer.get().drawInk((Graphics2D) arg0);
					if (getJCheckBoxShape().isSelected())
						SketchContainer.get().drawShapes((Graphics2D) arg0);

					StrokeContainer.get().draw((Graphics2D) arg0);
				}

			};
//			jPanel.setLayout(new GridBagLayout());
			jPanel.setBackground(Color.WHITE);

			jPanel.addMouseMotionListener(new MouseMotionListener() {

				public void mouseDragged(MouseEvent e) {
					if (getJToggleButtonDrawable().isSelected()) {
						StrokeContainer.get().appendPoint(new SampledPoint(e.getX(), e.getY()));
						StrokeContainer.get().draw((Graphics2D)getJPanel().getGraphics());
					}
				}

				public void mouseMoved(MouseEvent e) {
				}

			});

			jPanel.addMouseListener(new MouseAdapter() {

				@Override
				public void mousePressed(MouseEvent e) {
					super.mousePressed(e);

					if (getJToggleButtonDrawable().isSelected())
						StrokeContainer.get().appendPoint(new SampledPoint(e.getX(), e.getY()));
				}

				@Override
				public void mouseReleased(MouseEvent e) {
					super.mouseReleased(e);

					if (getJToggleButtonDrawable().isSelected()) {
						SketchContainer.get().open().appendStroke(StrokeContainer.get());
						StrokeContainer.reset();
						getJPanel().repaint();
					}
				}

			});
		}
		return jPanel;
	}

	/**
	 * This method initializes jToggleButtonDrawable
	 *
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getJToggleButtonDrawable() {
		if (jToggleButtonDrawable == null) {
			jToggleButtonDrawable = new JToggleButton();
			jToggleButtonDrawable.setText("Drawable");

			jToggleButtonDrawable.setSelected(true);
		}
		return jToggleButtonDrawable;
	}

	/**
	 * This method initializes jCheckBoxInk
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getJCheckBoxInk() {
		if (jCheckBoxInk == null) {
			jCheckBoxInk = new JCheckBox();
			jCheckBoxInk.setText("Show Ink");

			jCheckBoxInk.setSelected(true);
			jCheckBoxInk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getJPanel().repaint();
				}
			});
		}
		return jCheckBoxInk;
	}

	/**
	 * This method initializes jCheckBoxShape
	 *
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getJCheckBoxShape() {
		if (jCheckBoxShape == null) {
			jCheckBoxShape = new JCheckBox();
			jCheckBoxShape.setText("Show Shape");

			jCheckBoxShape.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getJPanel().repaint();
				}
			});
		}
		return jCheckBoxShape;
	}

	/**
	 * This method initializes jButtonCD
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCD() {
		if (jButtonCD == null) {
			jButtonCD = new JButton();
			jButtonCD.setText("CDAnalyzer");

			jButtonCD.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new CDResultViewer(MainFrame.this).setVisible(true);
				}
			});
		}
		return jButtonCD;
	}

	/**
	 * This method initializes jButtonSegment
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonSegment() {
		if (jButtonSegment == null) {
			jButtonSegment = new JButton();
			jButtonSegment.setText("Segment");
			jButtonSegment.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SketchContainer.get().segment();
					jPanel.repaint();
				}
			});
		}
		return jButtonSegment;
	}

	/**
	 * This method initializes jButtonParse
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonParse() {
		if (jButtonParse == null) {
			jButtonParse = new JButton();
			jButtonParse.setText("Parse");
			jButtonParse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("Before parsing ============");
					SketchContainer.get().open().print();
					SketchContainer.get().parse();
					SketchContainer.get().open().print();
					System.out.println("After parsing =============\n\n");
				}
			});
		}
		return jButtonParse;
	}

	/**
	 * This method initializes jButtonNew
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonNew() {
		if (jButtonNew == null) {
			jButtonNew = new JButton();
			jButtonNew.setText("New Document");
			jButtonNew.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SketchContainer.get().open().removeAll();
					jPanel.repaint();
				}
			});
		}
		return jButtonNew;
	}

	/**
	 * This method initializes jButtonPA
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonPA() {
		if (jButtonPA == null) {
			jButtonPA = new JButton();
			jButtonPA.setText("PrimApproximate");
			jButtonPA.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SketchContainer.get().primApproximate();
					getJCheckBoxInk().setSelected(false);
					getJCheckBoxShape().setSelected(true);
					getJPanel().repaint();
				}
			});
		}
		return jButtonPA;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}


		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				MainFrame thisClass = new MainFrame();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		initialize();

	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.setTitle("FinalSketch - A Rule-based Sketch Understanding System");
		this.setBounds(new Rectangle(50, 30, 900, 680));

		this.setResizable(false);
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("State show area");
			jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJToolBar(), BorderLayout.NORTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
			jContentPane.add(jLabel, BorderLayout.SOUTH);
		}
		return jContentPane;
	}


//	private CDParamListViewer viewer = null;
//
//	public CDParamListViewer getViewer() {
//		if (viewer == null) {
//			viewer = new CDParamListViewer(this);
//			viewer.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			viewer.setModal(true);
//		}
//		return viewer;
//	}
}
